package com.haiyi.software.app.basic;

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListDelegate;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshListView.PullToRefreshListAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public abstract class ActivityBasicGiftListView extends ActivityBasicGiftSend
		implements PullToRefreshListDelegate {

	protected PullToRefreshListView mPullRefreshListView;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	private void initComp() {
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(20)).build();
		mPullRefreshListView.setScrollingWhileRefreshingEnabled(false);
		mPullRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {
					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						String label = DateUtils.formatDateTime(MainApplication
								.getInstance().getApplicationContext(), System
								.currentTimeMillis(),
								DateUtils.FORMAT_SHOW_TIME
										| DateUtils.FORMAT_SHOW_DATE
										| DateUtils.FORMAT_ABBREV_ALL);
						// Update the LastUpdatedLabel
						refreshView.getLoadingLayoutProxy()
								.setLastUpdatedLabel(label);
						// Do work to refresh the list here.
						requestNetworkData();
					}
				});

		mPullRefreshListView
				.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

					@Override
					public void onLastItemVisible() {

						Toast.makeText(
								MainApplication.getInstance()
										.getApplicationContext(), "已经到底部了!",
								Toast.LENGTH_SHORT).show();
					}
				});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initComp();
		requestNetworkData();
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);

	}

	@Override
	public void requestFinish(JsonObject jsonObjectReturn) {

		mPullRefreshListView.setPullToRefreshListDelegate(this);

		mPullRefreshListView.setDataSource(data);

		mPullRefreshListView.setAdapter(null);

		mPullRefreshListView.getAdapterEx().notifyDataSetChanged();

		mPullRefreshListView.onRefreshComplete();

	}

	@Override
	public View bindView(PullToRefreshListAdapter adapter, int position,
			View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;

		if (convertView == null) {

			convertView = adapter.getLayoutInflater().inflate(
					R.layout.item_gift, null);
			viewHolder = new ViewHolder();
			viewHolder.item_i_content = (TextView) convertView
					.findViewById(R.id.item_i_content);
			viewHolder.item_i_info_count = (TextView) convertView
					.findViewById(R.id.item_i_info_count);
			viewHolder.item_i_photo = (ImageView) convertView
					.findViewById(R.id.item_i_photo);
			viewHolder.item_i_time = (Button) convertView
					.findViewById(R.id.item_i_time);
			viewHolder.item_i_title = (TextView) convertView
					.findViewById(R.id.item_i_title);
			convertView.setTag(viewHolder);

		} else {

			viewHolder = (ViewHolder) convertView.getTag();
		}

		viewHolder.item_i_content
				.setText(data.get(position).get("gfdesc") != null ? data
						.get(position).get("gfdesc").toString() : "");
		viewHolder.item_i_info_count
				.setText(data.get(position).get("gold") != null ? data
						.get(position).get("gold").toString() : "");
		viewHolder.item_i_title
				.setText(data.get(position).get("gfname") != null ? data
						.get(position).get("gfname").toString() : "");
		imageLoader.displayImage(str_server
				+ (data.get(position).get("gfpath") != null ? data
						.get(position).get("gfpath").toString() : ""),
				viewHolder.item_i_photo, options);

		Map<String, String> giftinfo = new HashMap<String, String>();

		giftinfo.put("gfid",
				data.get(position).get("gfid") != null ? data.get(position)
						.get("gfid").toString() : "");

		giftinfo.put("gold",
				data.get(position).get("gold") != null ? data.get(position)
						.get("gold").toString() : "");

		giftinfo.put("gfname", data.get(position).get("gfname") != null ? data
				.get(position).get("gfname").toString() : "");

		viewHolder.item_i_time.setTag(giftinfo);

		viewHolder.item_i_time.setOnClickListener(clickListener);

		return convertView;
	}

	Map<String, String> info;

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			info = (Map<String, String>) v.getTag();

			String msg = "确定要送 " + info.get("gfname") + " 给他吗？这将花费您 "
					+ info.get("gold") + " 个Hi币";

			Builder builder = new AlertDialog.Builder(
					ActivityBasicGiftListView.this);

			builder.setTitle("确认").setMessage(msg)
					.setPositiveButton("是", okClick)
					.setNegativeButton("否", cancelClick).show();

		}
	};

	private DialogInterface.OnClickListener okClick = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {

			Map<String, Object> argsBody = new HashMap<String, Object>();

			argsBody.put("gfid", info.get("gfid"));
			argsBody.put("touid", userUID);
			argsBody.put("conname", "礼物");

			sendGift(argsBody);

		}

	};

	private DialogInterface.OnClickListener cancelClick = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {

		}

	};

	private static class ViewHolder {
		ImageView item_i_photo;
		TextView item_i_title;
		TextView item_i_content;
		TextView item_i_info_count;
		Button item_i_time;
	}
}
